<?php
	require_once("utils.php");
	require_once("obj/project.php");
	require_once("obj/taskpriority.php");
	require_once("obj/task.php");
	require_once("obj/user.php");
	
	$taskid = $_GET["taskid"];
	$task = Task::getByTaskId($taskid);
	if(isset($_POST["btnSubmit"])){ 
		if($_POST["btnSubmit"] == "Save") {
			$task->ProjectId = $_POST["cboProject"];
			$task->Name = $_POST["txtName"];
			$task->Description = $_POST["txtDescription"];
			$task->TaskPriorityId = $_POST["cboPriority"];
			$task->AssignedUserId = $_POST["cboAssigned"];
			$task->save();
		}
		else if($_POST["btnSubmit"] == "Delete") {
			$task->delete();
		}
		
		$err = error_get_last();
		if (empty($err)) {
			$u = Utils::makeTasksUrl();
			header("Location:$u");
		}
	}
	
	$users = User::getAllUsers();
	$projects = Project::getForUser();
	$priorities = TaskPriority::getAll();
?>

<form method="post" action="<?php echo Utils::makeTaskDetailUrl($taskid);?>">
	<table cellpadding="5" cellspacing="0" border="0">
		<tr>
			<td>Assigned To:</td>
			<td>
				<select id="cboAssigned" name="cboAssigned">
					<option value="0">&nbsp;</option>
					<?php						
						foreach($users as $obj){
							$v = $obj->UserId;
							$n = $obj->Email;
							$s = ($v == $task->AssignedUserId) ? "selected='true'" : "";
							echo "<option value=\"$v\" $s>$n</option>";
						}
					?>
				</select>
			</td>
		</tr>
		<tr>
			<td>Project:</td>
			<td>
				<select id="cboProject" name="cboProject">
					<option value="0">&nbsp;</option>
					<?php						
						foreach($projects as $obj){
							$v = $obj->ProjectId;
							$n = $obj->Name;
							$s = ($v == $task->ProjectId) ? "selected='true'" : "";
							echo "<option value=\"$v\" $s>$n</option>";
						}
					?>
				</select>
			</td>
		</tr>
		<tr>
			<td>Priority:</td>
			<td>
				<select id="cboPriority" name="cboPriority">
					<option value="0">&nbsp;</option>
					<?php						
						foreach($priorities as $p){
							$v = $p->TaskPriorityId;
							$n = $p->Name;
							$s = ($v == $task->TaskPriorityId) ? "selected='true'" : "";
							echo "<option value=\"$v\" $s>$n</option>";
						}
					?>
				</select>
			</td>
		</tr>
		<tr>
			<td>Name:</td>
			<td><input type="text" name="txtName" id="txtName" value="<?php echo $task->Name; ?>" /></td>
		</tr>
		<tr>
			<td valign="top">Description:</td>
			<td>
				<textarea name="txtDescription" id="txtDescription"><?php echo $task->Description; ?></textarea>
			</td>
		</tr>
		<tr>
			<td colspan="2">
				<input name="btnSubmit" type="submit" value="Save" />
				<input name="btnSubmit" type="submit" value="Delete" />
				<input type="button" value="Cancel" onclick="javascript:history.back();" />
			</td>
		</tr>
	</table>
</form>